//
//  YYLHomePictureView.swift
//  XinlangWeibo
//
//  Created by yangyinglei on 2017/4/1.
//  Copyright © 2017年 yangyinglei. All rights reserved.
//

import UIKit

//格子间距
let itemMargin: CGFloat = 5
//格子宽度
let itemWidth: CGFloat = (UIScreen.main.bounds.size.width - 2 * homeCellMargin - 2 * itemMargin) / 3

class YYLHomePictureView: UICollectionView {

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowLayout = UICollectionViewFlowLayout()
        super.init(frame: frame, collectionViewLayout: flowLayout)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        self.register(YYLPictureCollectionViewCell.self, forCellWithReuseIdentifier: homePictureItemIdentifier)
        //设置数据源
        dataSource = self
    
    }
    
    // 通过模型赋值
    var picUrls: [YYLHomePictureInfo]? {
        didSet{
            
            //设置label的内容
            let num = picUrls?.count ?? 0

            //设置View大小
        calculateSize(number: num)
            reloadData()
        }
    
    }

    //  懒加载各个控件
    //  显示图片个数
    
    //计算view的大小  和 内部图片的大小
    fileprivate func calculateSize(number: Int) {

        //设置布局
        let flowLayout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        flowLayout.minimumLineSpacing = itemMargin
        flowLayout.minimumInteritemSpacing = itemMargin
        
        //计算列数
        let cols = number > 3 ? 3 : number
        //行数
        let rows = (number - 1) / 3 + 1
    
        //计算view的大小
        let currentWidth = CGFloat(cols) * itemWidth + CGFloat(cols - 1) * itemMargin
        let currentHeight = CGFloat(rows) * itemWidth + CGFloat(rows - 1) * itemMargin
        
        //跟新自身约束
        self.snp.updateConstraints { (make) in
            
            make.size.equalTo(CGSize(width: currentWidth, height: currentHeight))
        }
    }
}



//MARK:------CollectionView的代理的实现
extension YYLHomePictureView: UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picUrls?.count ?? 0
        
    }
    
    
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: homePictureItemIdentifier, for: indexPath) as! YYLPictureCollectionViewCell
        
        cell.pictureInfo = picUrls![indexPath.item]
        return cell
    }

}



//MARK:-----自定义cell
class YYLPictureCollectionViewCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {

        contentView.addSubview(imageView)
        
        imageView.snp.makeConstraints { (make) in
            make.edges.equalTo(contentView).offset(0)
        }
    }
    
    //懒加载控件
    lazy var imageView: UIImageView = {
        let img = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        img.contentMode = .scaleAspectFill
        img.clipsToBounds = true
        return img
    }()
    
    //绑定数据
    var pictureInfo: YYLHomePictureInfo? {
        didSet{
            if let urlStr = pictureInfo?.thumbnail_pic {
                imageView.sd_setImage(with: URL(string: urlStr), placeholderImage: UIImage(named: "timeline_image_placeholder"))
            }
        }

    }
    
}
